<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../css/aui/aui.css" />
		<link rel="stylesheet" type="text/css" href="../../css/winu-ui.css" />
		<style type="text/css">
			.title-area {
				padding: 10px 15px 25px 15px;
				background: #1d9dd5;
			}
			.title-area p {
				font-size: 26px;
				color: #fff;
				font-weight: bold;
			}
			.title-area span {
				display: block;
				font-size: 14px;
				color: #fff;
			}
			.content {
				padding: 15px;
				line-height: 30px;
				font-size: 16px;
				color: #444;
			}
			.display-none {
				display: none;
			}
		</style>
		<script id="tpl" type="text/html">
			<div class="title-area">
			<p>
			[:=data.title:]
			</p>
			<span>[:=(data.user_info).user_name:]&nbsp;&nbsp;&nbsp;[:=transTime(data.add_time):]</span>
			</div>
			<div class="content">
			[:=data.message:]
			</div>
		</script>
	</head>
	<body>
		<div class="aui-tips aui-tips-danger display-none">
			<div class="aui-tips-content aui-ellipsis-1">
				<i class="aui-iconfont aui-icon-warnfill"></i>
				世界上最遥远的距离就是没网。 <i class="aui-iconfont aui-icon-roundclosefill" onclick="$(this).parents('.aui-tips').addClass('display-none');"></i>
			</div>
		</div>
		<div id="bbs-container"></div>
	</body>
	<script type="text/javascript" src="../../script/AHelper.js"></script>
	<script type="text/javascript" src="../../script/zepto.min.js"></script>
	<script type="text/javascript" src="../../script/date.js"></script>
	<script type="text/javascript" src="../../script/app.js"></script>
	<script type="text/javascript">
		// 合并问题和文章
		function createTplData(obj, postType) {
			var newObj = {};
			if (postType == "question") {
				newObj.id = obj.question_id;
				newObj.title = obj.question_content;
				newObj.message = obj.question_detail;
				newObj.add_time = obj.add_time;
				newObj.post_type = obj.post_type;
				newObj.answer_count = obj.answer_count;
				newObj.view_count = obj.view_count;
				newObj.agree_count = obj.agree_count;
				newObj.user_info = obj.user_info;
				newObj.topics = obj.topics;
			} else {
				newObj.id = obj.id;
				newObj.title = obj.title;
				newObj.message = obj.message;
				newObj.add_time = obj.add_time;
				newObj.post_type = obj.post_type;
				newObj.answer_count = obj.comments;
				newObj.view_count = obj.views;
				newObj.agree_count = obj.votes;
				newObj.user_info = obj.user_info;
				newObj.topics = obj.topics;
			}
			return newObj;
		}

		function loadData(id, postType) {
			// 加载
			H.$openFrame('loading', 'widget://res/load9.html', null, {
				x : 0,
				y : 0
			});
			var url = "";
			if (postType == "question") {
				url = window.apiUrl + 'question/?id=' + id + '&sort_key=add_time';
			} else {
				url = window.apiUrl + 'article/?id=' + id;
			}
			H.$ajax(function(ret, err) {
				// 有数据
				if (ret.rsm) {
					var newObj = createTplData((postType == "question" ? ((ret.rsm).question_info) : ((ret.rsm).article_info)), postType);
					// 获取模板内容，并加载数据
					var html = H.$tppl(H.$api.byId('tpl').innerHTML, {
						data : newObj
					});
					$("#bbs-container").html(html);
					H.$execScript(null, null, 'setNavData(' + newObj.agree_count + ',' + newObj.answer_count + ');');
				}
				H.closeFrame("loading");
			}, url);
		}


		H.ready(function() {
			var param = H.$getPageParam();
			if (api.connectionType != "none") {
				loadData(param.id, param.postType);
			} else {
				H.closeFrame("loading");
				$(".aui-tips").removeClass("display-none");
			}
			// 监听网络
			H.$offline(function(ret, err) {
				$(".aui-tips").removeClass("display-none");
			});
			H.$online(function(ret, err) {
				$(".aui-tips").addClass("display-none");
				loadData(param.id, param.postType);
			});
		});
	</script>
</html>